package com.sharp.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Title: RabbitDelayMessageConfig
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2021/7/2 14:39
 */
@Configuration
public class RabbitDelayMessageConfig {

    public static final String DELAY_MESSAGE_ROUTER_KEY = "DELAY_MESSAGE_ROUTER_KEY";
    public static final String DELAY_MESSAGE_PLUGIN_EXCHANGE_NAME = "delayPlugInExchange";
    public static final String DELAY_MESSAGE_PLUGIN_QUEUE_NAME = "delayPlugInMessageQueue";
    public static final String DELAY_MESSAGE_EXCHANGE_NAME = "delayMessageExchange";
    public static final String DELAY_MESSAGE_QUEUE_NAME = "delayMessageReplyQueue";

    /**
     * 基于延迟消息插件交换机
     */
    @Bean
    public CustomExchange delayPlugInExchange() {
        //创建一个自定义交换机，可以发送延迟消息
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAY_MESSAGE_PLUGIN_EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    /**
     * 基于延迟消息插件消息队列
     */
    @Bean
    public Queue delayPlugInMessageQueue() {
        return new Queue(DELAY_MESSAGE_PLUGIN_QUEUE_NAME, true);
    }

    /**
     * 基于延迟消息插件消息队列交换机绑定
     */
    @Bean
    public Binding delayPlugInMessageBinding(CustomExchange delayPlugInExchange, Queue delayPlugInMessageQueue) {
        return BindingBuilder.bind(delayPlugInMessageQueue).to(delayPlugInExchange).with(DELAY_MESSAGE_ROUTER_KEY).noargs();
    }


    /**
     * 基于死信机制死信交换机
     */
    @Bean
    public DirectExchange delayMessageDealExchange() {
        return new DirectExchange(DELAY_MESSAGE_EXCHANGE_NAME, true, true);
    }

    /**
     * 基于死信机制回复交换机
     */
    @Bean
    public DirectExchange delayMessageReplyExchange() {
        return new DirectExchange("delayMessageReplyExchange", true, true);
    }

    /**
     * 基于死信机制死信消息队列
     */
    @Bean
    public Queue delayMessageDealQueue() {
        // reply_to 队列
        Map<String, Object> map = new HashMap<>();
        //设置消息的过期时间 单位毫秒
        // map.put("x-message-ttl", 10000L);
        //设置附带的死信交换机
        map.put("x-dead-letter-exchange", "delayMessageReplyExchange");
        //指定重定向的路由建 消息作废之后可以决定需不需要更改他的路由建 如果需要 就在这里指定
        map.put("x-dead-letter-routing-key", DELAY_MESSAGE_ROUTER_KEY);
        return new Queue("delayMessageDealQueue", true, false, true, map);
    }

    /**
     * 基于死信机制回复消息队列
     */
    @Bean
    public Queue delayMessageReplyQueue() {
        return new Queue(DELAY_MESSAGE_QUEUE_NAME, true);
    }

    /**
     * 基于死信机制交换机绑定
     */

    @Bean
    public Binding delayMessageDealBinding(DirectExchange delayMessageDealExchange, Queue delayMessageDealQueue) {
        return BindingBuilder.bind(delayMessageDealQueue).to(delayMessageDealExchange).with(DELAY_MESSAGE_ROUTER_KEY);
    }

    @Bean
    public Binding delayMessageReplyBinding(DirectExchange delayMessageReplyExchange, Queue delayMessageReplyQueue) {
        return BindingBuilder.bind(delayMessageReplyQueue).to(delayMessageReplyExchange).with(DELAY_MESSAGE_ROUTER_KEY);
    }

}
